package com.heiyu.api.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.heiyu.api.controller.dto.PicInfo;
import com.heiyu.api.request.*;
import com.heiyu.api.response.*;
import com.heiyu.api.response.market.list.*;
import com.heiyu.api.response.model.*;
import com.heiyu.constants.BannerPositionEnum;
import com.heiyu.controller.ApiBaseController;
import com.heiyu.dao.*;
import com.heiyu.entity.*;
import com.heiyu.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mountain on 18/6/11.
 */
@Api(value = "榜单模块",description = "榜单接口")
@RestController
@RequestMapping("api/v1/market")
public class MarketInfoController extends ApiBaseController {

    @Autowired
    private BannerInfoMapper bannerInfoMapper;
    @Autowired
    private GlobalShowInfoMapper globalShowInfoMapper;
    @Autowired
    private CoinBaseInfoMapper coinBaseInfoMapper;
    @Autowired
    private ShowInfoMapper showInfoMapper;
    @Autowired
    private MarketBaseInfoMapper marketBaseInfoMapper;
    @Autowired
    private MarketNoticeMapper marketNoticeMapper;
    @Autowired
    private MarketCoinCoinMapper marketCoinCoinMapper;
    @Autowired
    private ArticleInfoMapper articleInfoMapper;
    @Autowired
    private ArticleLikeMapper articleLikeMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @ApiOperation(value = "list",notes = "首页榜单列表")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public MarketListResponse list(@ModelAttribute MarkertListRequest request){
        MarketListResponse response = new MarketListResponse();
        response.success();
        MarketListModel model = new MarketListModel();
        //获取banner数据
        BannerInfo bannerInfo = new BannerInfo();
        bannerInfo.setPosition(BannerPositionEnum.INDEX);
        List<BannerInfo> bannerInfoList = this.bannerInfoMapper.selectList(bannerInfo);
        model.setBannerList(new ArrayList<BannerInfoIndex>());
        if(!CollectionUtils.isEmpty(bannerInfoList)){
            BannerInfoIndex bannerInfoIndex;
            for(BannerInfo bannerEntity:bannerInfoList){
                bannerInfoIndex = new BannerInfoIndex();
                bannerInfoIndex.setImgUrl(bannerEntity.getImgUrl());
                bannerInfoIndex.setTargetUrl(bannerEntity.getTargetUrl());
                bannerInfoIndex.setTitle(bannerEntity.getTitle());
                model.getBannerList().add(bannerInfoIndex);
            }
        }
        //获取新闻消息
        bannerInfo.setPosition(BannerPositionEnum.INDEX_NEWS);
        model.setNewsList(Lists.newArrayList());
        bannerInfoList = this.bannerInfoMapper.selectList(bannerInfo);
        if(!CollectionUtils.isEmpty(bannerInfoList)){
            ArticleEntity articleEntity;
            ArticleInfo entity;
            ArticleLike like = new ArticleLike();
            UserInfo userInfo;
            for(BannerInfo bannerEntity:bannerInfoList){
                entity = this.articleInfoMapper.selectPrimary(Long.valueOf(bannerEntity.getTargetUrl().trim()));
                if(entity == null){
                    continue;
                }
                articleEntity = new ArticleEntity();
                userInfo = this.userInfoMapper.selectPrimary(entity.getUserId());
                like.setArticleId(entity.getId());
                articleEntity.setArticleId(entity.getId());
                if(userInfo != null){
                    articleEntity.setAuthor(userInfo.getNickName());
                }
                articleEntity.setTitle(entity.getTitle());
                articleEntity.setTimestamp(Integer.valueOf(entity.getCreatedTime().getTime()/1000+""));
                like.setIsGood("1");
                articleEntity.setGoodNum(articleLikeMapper.loveCount(like));
                like.setIsGood("0");
                articleEntity.setBadNum(articleLikeMapper.loveCount(like));
                articleEntity.setImgUrl(StringUtils.isEmpty(entity.getImgUrl())?DefaultImgUtils.getDefaultImg():entity.getImgUrl());
                model.getNewsList().add(articleEntity);
            }
        }
        //获取BTC和ETC的行情数据
        List<String> symbols = new ArrayList<String>();
        symbols.add("BTC");
        symbols.add("ETH");
        List<GlobalShowInfo> btcEthInfo = globalShowInfoMapper.selectBySymbols(symbols);
        MarketListMarketInfo marketListMarketInfo;
        for(GlobalShowInfo globalShowInfo:btcEthInfo){
            marketListMarketInfo = new MarketListMarketInfo();
            if(defaultSymbol()){
                marketListMarketInfo.setPrice(MoneyUtils.formatter(globalShowInfo.getPriceCny(),false));
            }else{
                marketListMarketInfo.setPrice(MoneyUtils.formatter(globalShowInfo.getPriceUsd(),false));
            }
            marketListMarketInfo.setRange(RangeUtil.formatter(globalShowInfo.getPercentChange24h()));
            marketListMarketInfo.setTargetId(globalShowInfo.getTargetId());
            marketListMarketInfo.setSymbol(globalShowInfo.getSymbol());
            if(globalShowInfo.getSymbol().toLowerCase().equals("btc")){
                model.setBtcMarketInfo(marketListMarketInfo);
            }else{
                model.setEthMarketInfo(marketListMarketInfo);
            }
        }
        //获取24小时涨幅
        Map<String,Object> param = new HashMap<String,Object>();
        param.put("desc","desc");
        param.put("index",0);
        param.put("offset",4);
        List<GlobalShowInfo> upGlobalInfoList = this.globalShowInfoMapper.select24HChange(param);
        model.setUpList(formatUpDown(upGlobalInfoList));
        //获取24小时跌幅
        param.remove("desc");
        param.put("desc","asc");
        List<GlobalShowInfo> downGlobalInfoList = this.globalShowInfoMapper.select24HChange(param);
        model.setDownList(formatUpDown(downGlobalInfoList));
        //获取24小时成交榜
        if(!StringUtils.isEmpty(request.getTardePriceType())){
            param.put("tradePriceDesc",request.getTardePriceType().equals("U")?"desc":"asc");
        }
        if(!StringUtils.isEmpty(request.getTradeAmountType())){
            param.put("tradeAmountDesc",request.getTradeAmountType().equals("U")?"desc":"asc");
        }
        List<GlobalShowInfo> tradeAmountList = this.globalShowInfoMapper.selectByOrderDesc(param);
        model.setTradeList(formatTradeAmountList(tradeAmountList));
        //获取24小时资金流向
        param.remove("tradePriceDesc");
        param.remove("tradeAmountDesc");
        if(!StringUtils.isEmpty(request.getNetFlowType())){
            param.put("netFlowType",request.getNetFlowType().equals("U")?"desc":"asc");
        }
        if(!StringUtils.isEmpty(request.getFlowPriceType())){
            param.put("netFlowPriceType",request.getFlowPriceType().equals("U")?"desc":"asc");
        }
        List<GlobalShowInfo> netflowList = this.globalShowInfoMapper.selectByOrderDesc(param);
        model.setFundList(formatFundList(netflowList));
        //获取30天内的新币
        param.remove("netFlowType");
        param.remove("netFlowPriceType");
        if(!StringUtils.isEmpty(request.getNewPriceType())){
            param.put("newPriceType",request.getNewPriceType().equals("U")?"desc":"asc");
        }
        if(!StringUtils.isEmpty(request.getNewPublishType())){
            param.put("publishTimeType",request.getNewPublishType().equals("U")?"desc":"asc");
        }
        List<CoinBaseInfo> coinBaseInfos = this.coinBaseInfoMapper.selectNewList(param);
        if(CollectionUtils.isEmpty(coinBaseInfos)){
            model.setNewList(new ArrayList<>());
        }else{
            GlobalShowInfo showInfo = new GlobalShowInfo();
            List<GlobalShowInfo> newInfoList = Lists.newArrayList();
            for(CoinBaseInfo coinBaseInfo:coinBaseInfos){
                showInfo.setTargetId(coinBaseInfo.getTargetId());
                try{
                    newInfoList.add(globalShowInfoMapper.selectList(showInfo).get(0));
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
            //按照价格排序
            model.setNewList(formatNewList(newInfoList));
        }
        response.setData(model);
        return response;
    }


    @ApiOperation(value = "type/list",notes = "具体榜单信息列表")
    @RequestMapping(value = "type/list",method = RequestMethod.GET)
    public MarketTypeListResponse typeList(@ModelAttribute("request") MarketTypeListRequest request){
        MarketTypeListResponse response = new MarketTypeListResponse();
        MarketMoreListModel model = new MarketMoreListModel();
        response.success();
        Map<String,Object> param = PageUtils.newParam(request.getPageIndex(),request.getPageSize());
        //榜单类型,1是24小时成交，2是24资金流向，3是新币榜单,4涨跌幅排行榜，5全球榜单数据
        switch (request.getType()){
            case "1":
                if(!StringUtils.isEmpty(request.getPriceType())){
                    param.put("tradePriceDesc",request.getPriceType().equals("U")?"desc":"asc");
                }
                if(!StringUtils.isEmpty(request.getTradeAmountType())){
                    param.put("tradeAmountDesc",request.getTradeAmountType().equals("U")?"desc":"asc");
                }
                List<GlobalShowInfo> tradeAmountList = this.globalShowInfoMapper.selectByOrderDesc(param);
                model.setList(formatMarketMoreList(tradeAmountList));
                break;
            case "2":
                if(!StringUtils.isEmpty(request.getNetFlowType())){
                    param.put("netFlowType",request.getNetFlowType().equals("U")?"desc":"asc");
                }
                if(!StringUtils.isEmpty(request.getPriceType())){
                    param.put("netFlowPriceType",request.getPriceType().equals("U")?"desc":"asc");
                }
                List<GlobalShowInfo> netflowList = this.globalShowInfoMapper.selectByOrderDesc(param);
                model.setList(formatMarketMoreList(netflowList));
                break;
            case "3":
                if(!StringUtils.isEmpty(request.getPriceType())){
                    param.put("newPriceType",request.getPriceType().equals("U")?"desc":"asc");
                }
                if(!StringUtils.isEmpty(request.getPublishTimeType())){
                    param.put("publishTimeType",request.getPublishTimeType().equals("U")?"desc":"asc");
                }
                List<CoinBaseInfo> coinBaseInfos = this.coinBaseInfoMapper.selectNewList(param);
                if(CollectionUtils.isEmpty(coinBaseInfos)){
                    model.setList(Lists.newArrayList());
                }else{
                    List<GlobalShowInfo> newInfoList = Lists.newArrayList();
                    GlobalShowInfo globalShowInfo  = new GlobalShowInfo();
                    for(CoinBaseInfo info:coinBaseInfos){
                        globalShowInfo.setTargetId(info.getTargetId());
                        newInfoList.add(globalShowInfoMapper.selectList(globalShowInfo).get(0));
                    }
                    model.setList(formatMarketMoreList(newInfoList));
                }
                break;
            case "4":
                if(!StringUtils.isEmpty(request.getPriceType())){
                    param.put("tradePriceDesc",request.getPriceType().equals("U")?"desc":"asc");
                }
                if(!StringUtils.isEmpty(request.getRangeType())){
                    param.put("rangeDesc",request.getRangeType().equals("U")?"desc":"asc");
                }
                if(!StringUtils.isEmpty(request.getValueType())){
                    param.put("valueType",request.getValueType().equals("U")?"desc":"asc");
                }
                if(!StringUtils.isEmpty(request.getRangTargetType())){
                    param.put("rangJust",request.getRangTargetType().equals("U")?"U":"D");
                }
                List<GlobalShowInfo> rangeList = this.globalShowInfoMapper.selectByOrderDesc(param);
                model.setList(formatMarketMoreList(rangeList));
                break;
        }
        response.setData(model);
        return response;
    }

    @ApiOperation(value="info/list",notes = "交易所列表")
    @RequestMapping(value = "info/list",method = RequestMethod.GET)
    public MarketInfoListResponse infoList(@ModelAttribute("request")MarketInfoListRequest request){
        MarketInfoListResponse response = new MarketInfoListResponse();
        response.success();
        MarkerInfoListModel model = new MarkerInfoListModel();
        model.setList(Lists.newArrayList());
        Map<String,Object> param = PageUtils.newParam(request.getPageIndex(),request.getPageSize());
        if(!StringUtils.isEmpty(request.getTradeType())){
            param.put("type",Integer.valueOf(request.getTradeType()));
        }
        if(!StringUtils.isEmpty(request.getPlace())){
            param.put("place",request.getPlace());
        }
        if(!StringUtils.isEmpty(request.getTradeAmountType())){
            param.put("tradeAmountType",request.getTradeAmountType().equals("U")?"desc":"asc");
        }
        List<MarketBaseInfo> list = this.marketBaseInfoMapper.selectForCon(param);
        if(!CollectionUtils.isEmpty(list)){
            MarketInfo marketInfo;
            for(MarketBaseInfo baseInfo:list){
                marketInfo = new MarketInfo();
                marketInfo.setCoinPairNum(baseInfo.getPairNum());
                marketInfo.setLogoUrl(baseInfo.getLogo());
                marketInfo.setName(baseInfo.getMarketName());
                marketInfo.setNo(baseInfo.getRank());
                if(defaultSymbol()){
                    marketInfo.setTradeAmount(MoneyUtils.formatter(baseInfo.getVol24hCny(),true));
                }else{
                    marketInfo.setTradeAmount(MoneyUtils.formatter(baseInfo.getVol24hUsd(),true));
                }

                marketInfo.setSupportType(MarketSupportTypeUtils.getSupportType(baseInfo.getType()));
                marketInfo.setTargetId(baseInfo.getId()+"");
                marketInfo.setPlace(baseInfo.getCountry());
                model.getList().add(marketInfo);
            }
        }
        response.setData(model);
        return response;
    }



    @ApiOperation(value = "info/top5/list",notes = "获取交易量前5的交易所")
    @RequestMapping(value = "info/top5/list",method = RequestMethod.GET)
    public MarketTop5Response top5(){
        MarketTop5Response response = new MarketTop5Response();
        response.success();
        response.setData(new MarketTop5Model());
        Map<String,Object> param = PageUtils.newParam(1,5);
        param.put("tradeAmountType","desc");
        List<MarketBaseInfo> list = this.marketBaseInfoMapper.selectForCon(param);
        if(!CollectionUtils.isEmpty(list)){
            Map<String,String> map = new HashMap<String,String>();
            for(MarketBaseInfo baseInfo:list){
                map.put(baseInfo.getId()+"",baseInfo.getAlias());
            }
            response.getData().setData(map);
        }
        return response;
    }

    @ApiOperation(value = "获取参数列表",notes = "获取筛选参数")
    @ApiImplicitParam(value = "交易所主键",name = "marketId")
    @RequestMapping(value = "filter/list",method = RequestMethod.GET)
    public MarketFilterResponse filterList(@RequestParam(required = false) String marketId,@RequestParam(required = false)String symbol){
        MarketFilterResponse response = new MarketFilterResponse();
        response.success();
        List<String> list = this.marketBaseInfoMapper.getAllPlace();
        response.setData(new MarketFilterModel());
            if(!CollectionUtils.isEmpty(list)){
                response.getData().setPlaceList(Lists.newArrayList());
                FilterInfo info;
                for(String place:list){
                    info = new FilterInfo();
                    if(StringUtils.isEmpty(place)){
                        info.setName("全国");
                        info.setValue("");
                    }else {
                        info.setName(place);
                        info.setValue(place);
                    }
                    response.getData().getPlaceList().add(info);
                }
            }
            response.getData().setSupportTypeList(Lists.newArrayList());
            FilterInfo info = new FilterInfo();
            info.setName("法币");
            info.setValue("1");
            response.getData().getSupportTypeList().add(info);
             info = new FilterInfo();
            info.setName("期货合同");
            info.setValue("2");
            response.getData().getSupportTypeList().add(info);
             info = new FilterInfo();
            info.setName("现货");
            info.setValue("4");
            response.getData().getSupportTypeList().add(info);

            response.getData().setSupportExchangeList(Lists.newArrayList());
            List<String> exchangeCoin;
            if(StringUtils.isEmpty(symbol)){
                MarketCoinCoin coinCoin = new MarketCoinCoin();
                if(!StringUtils.isEmpty(marketId)){
                    MarketBaseInfo marketBaseInfo  = this.marketBaseInfoMapper.selectPrimary(Long.valueOf(marketId));
                    coinCoin.setMarketName(marketBaseInfo.getBlockccName());
                }
                exchangeCoin = this.marketCoinCoinMapper.selectSupportExchangeCoin(coinCoin);
            }else{
                exchangeCoin = this.marketCoinCoinMapper.selectTargetSupporyExchangeCoin(symbol);
            }

            if(!CollectionUtils.isEmpty(exchangeCoin)){
                for(String str : exchangeCoin){
                    info = new FilterInfo();
                    info.setValue(str);
                    info.setName(str);
                    response.getData().getSupportExchangeList().add(info);
                }
            }
            return response;
    }

    @ApiOperation(value="info/get",notes = "交易所详情信息")
    @ApiImplicitParam(name = "targetId",value = "交易所主键")
    @RequestMapping(value = "info/get",method = RequestMethod.GET)
    public MarketInfoDetailResponse infoGet(@RequestParam String targetId){
        MarketInfoDetailResponse response = new MarketInfoDetailResponse();
        response.success();
        MarketBaseInfo baseInfo = this.marketBaseInfoMapper.selectPrimary(Long.valueOf(targetId));
        if(baseInfo== null){
            response.error("市场信息不存在");
            return response;
        }
        MarketInfoDetailModel model = new MarketInfoDetailModel();
        model.setCoinPairNum(baseInfo.getPairNum());
        model.setContent(baseInfo.getDescLong());
        model.setName(baseInfo.getMarketName());
        model.setNo(baseInfo.getRank());
        model.setPlace(baseInfo.getCountry());
        if(defaultSymbol()){
            model.setTradeAmount(MoneyUtils.formatter(baseInfo.getVol24hCny(),true));
        }else{
            model.setTradeAmount(MoneyUtils.formatter(baseInfo.getVol24hUsd(),true));
        }

        model.setTargetId(baseInfo.getId()+"");

        model.setSupportTypeList(MarketSupportTypeUtils.getSupportType(baseInfo.getType()));
        model.setUrl(baseInfo.getWebsite());
        model.setFeature(baseInfo.getFeatureCn());
        model.setPicPontList(Lists.newArrayList());
        model.setBasePicPontList(Lists.newArrayList());
        //交易量占比
        String objStr = redisUtils.get("G_MARKET_PAIR_PERCENT_"+model.getName());
        if(objStr != null){
            PicInfo info =  JSON.parseObject(objStr,PicInfo.class);
            PicPointInfo picPointInfo;
            for(int i=0;i<info.getKey().size();i++){
                picPointInfo = new PicPointInfo();
                picPointInfo.setName(info.getKey().get(i));
                picPointInfo.setPrecentDesc(RangeUtil.formatterNoFlag(info.getValue().get(i)));
                picPointInfo.setPrecent(info.getValue().get(i).multiply(new BigDecimal("100")).intValue());
                model.getPicPontList().add(picPointInfo);
            }
        }
        //基础货币占比
        objStr = redisUtils.get("G_MARKET_BASE_PAIR_PERCENT_"+model.getName());
        if(objStr != null){
            PicInfo info =  JSON.parseObject(objStr,PicInfo.class);
            PicPointInfo picPointInfo;
            for(int i=0;i<info.getKey().size();i++){
                picPointInfo = new PicPointInfo();
                picPointInfo.setName(info.getKey().get(i));
                picPointInfo.setPrecentDesc(RangeUtil.formatterNoFlag(info.getValue().get(i)));
                picPointInfo.setPrecent(info.getValue().get(i).multiply(new BigDecimal("100")).intValue());
                model.getBasePicPontList().add(picPointInfo);
            }
        }
        response.setData(model);
        return response;
    }

    @ApiOperation(value = "pair/list",notes = "交易所交易对列表")
    @RequestMapping(value = "pair/list",method = RequestMethod.GET)
    public CoinPairInfoListResponse coinPairList(@ModelAttribute("request")CoinPairInfoListRequest request){
        CoinPairInfoListResponse response = new CoinPairInfoListResponse();
        response.success();
        MarketBaseInfo  baseInfo = null;
        if(!StringUtils.isEmpty(request.getTargetId())){
            baseInfo = this.marketBaseInfoMapper.selectPrimary(Long.valueOf(request.getTargetId()));
        }

        Map<String,Object> param = PageUtils.newParam(request.getPageIndex(), request.getPageSize());
        if(baseInfo != null){
            param.put("targetId",baseInfo.getMarketName());
        }
        if(!StringUtils.isEmpty(request.getExchangeTargetId()))
        param.put("exchangeTargetId",request.getExchangeTargetId());
        if(!StringUtils.isEmpty(request.getPriceType()))
        param.put("priceDesc",request.getPriceType().equals("U")?"desc":"asc");
        if(!StringUtils.isEmpty(request.getRangeType()))
        param.put("rangeDesc",request.getRangeType().equals("U")?"desc":"asc");

        if(!(param.containsKey("priceDesc")||param.containsKey("rangeDesc"))){
            param.put("tradeDesc","desc");
        }
        List<ShowInfo> list  = this.showInfoMapper.selectMarketList(param);
        response.setData(new CoinPairInfoListModel());
        response.getData().setList(new ArrayList<CoinPairInfo>());
        if(!CollectionUtils.isEmpty(list)){
            CoinPairInfo coinPairInfo;
            for(ShowInfo info:list){
                coinPairInfo = new CoinPairInfo();
                coinPairInfo.setCoinName(info.getSymbol());
                coinPairInfo.setExchangeCoinName(info.getTCoin());
                coinPairInfo.setExchangePirce(MoneyUtils.formatterCoin(info.getPriceNative()));
                if(defaultSymbol()){
                    coinPairInfo.setPrice(MoneyUtils.formatter(info.getPriceCny(),false));
                    coinPairInfo.setTradeAmount(MoneyUtils.formatter(info.getVol24hCny(),true));
                }else{
                    coinPairInfo.setPrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                    coinPairInfo.setTradeAmount(MoneyUtils.formatter(info.getVol24hUsd(),true));
                }
                if(baseInfo != null){
                    coinPairInfo.setHasPic(hasPic(info.getSymbol(),info.getTCoin(),baseInfo.getBlockccName()));
                }else{
                    coinPairInfo.setHasPic(hasPic(info.getSymbol(),info.getTCoin(),info.getMarketName()));
                }

                coinPairInfo.setKlinePicId(request.getTargetId());
                coinPairInfo.setMarketName(info.getMarketName());
                coinPairInfo.setRange(RangeUtil.formatter(info.getPercentChange24h()));
                coinPairInfo.setTargetId(info.getId()+"");
                response.getData().getList().add(coinPairInfo);
            }
        }
        return response;
    }

    @ApiOperation(value = "target/pair/list",notes = "币种交易对列表")
    @RequestMapping(value = "target/pair/list",method = RequestMethod.GET)
    public CoinPairInfoListResponse targetCoinPairList(@ModelAttribute("request")CoinPairInfoListRequest request){
        CoinPairInfoListResponse response = new CoinPairInfoListResponse();
        response.success();
        Map<String,Object> param = PageUtils.newParam(request.getPageIndex(), request.getPageSize());
        if(request.getTargetId() != null){
            CoinBaseInfo coinBaseInfo = coinBaseInfoUtils.get(request.getTargetId());
            param.put("usName",coinBaseInfo.getUsName());
        }
        if(!StringUtils.isEmpty(request.getExchangeTargetId()))
        param.put("exchangeTargetId",request.getExchangeTargetId());

        if(!StringUtils.isEmpty(request.getPriceType()))
            param.put("priceDesc",request.getPriceType().equals("U")?"desc":"asc");
        if(!StringUtils.isEmpty(request.getRangeType()))
            param.put("rangeDesc",request.getRangeType().equals("U")?"desc":"asc");

        if(!(param.containsKey("priceDesc")||param.containsKey("rangeDesc"))){
            param.put("tradeDesc","desc");
        }
        List<ShowInfo> list  = this.showInfoMapper.selectMarketList(param);
        response.setData(new CoinPairInfoListModel());
        response.getData().setList(new ArrayList<CoinPairInfo>());
        if(!CollectionUtils.isEmpty(list)){
            CoinPairInfo coinPairInfo;
            MarketBaseInfo marketBaseInfo = new MarketBaseInfo();
            List<MarketBaseInfo> marketBaseInfoList;
            for(ShowInfo info:list){
                coinPairInfo = new CoinPairInfo();
                coinPairInfo.setCoinName(info.getSymbol());
                coinPairInfo.setExchangeCoinName(info.getTCoin());
                coinPairInfo.setExchangePirce(MoneyUtils.formatterCoin(info.getPriceNative()));
                if(defaultSymbol()){
                    coinPairInfo.setPrice(MoneyUtils.formatter(info.getPriceCny(),false));
                    coinPairInfo.setTradeAmount(MoneyUtils.formatter(info.getVol24hCny(),true));
                }else{
                    coinPairInfo.setPrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                    coinPairInfo.setTradeAmount(MoneyUtils.formatter(info.getVol24hUsd(),true));
                }
                marketBaseInfo.setMarketName(info.getMarketName());
                marketBaseInfoList = this.marketBaseInfoMapper.selectList(marketBaseInfo);
                if(!CollectionUtils.isEmpty(marketBaseInfoList)){
                    coinPairInfo.setHasPic(hasPic(info.getSymbol(),info.getTCoin(),marketBaseInfoList.get(0).getBlockccName()));
                }else{
                    coinPairInfo.setHasPic("N");
                }
                coinPairInfo.setKlinePicId(request.getTargetId());
                coinPairInfo.setMarketName(info.getMarketName());
                coinPairInfo.setRange(RangeUtil.formatter(info.getPercentChange24h()));
                coinPairInfo.setTargetId(info.getId()+"");

                response.getData().getList().add(coinPairInfo);
            }
        }
        return response;
    }

    @ApiOperation(value = "note/list",notes = "交易所公告列表")
    @RequestMapping(value = "note/list",method = RequestMethod.GET)
    public NoteInfoListResponse noteList(@ModelAttribute("request")NoteListRequest request){
       MarketBaseInfo baseInfo = this.marketBaseInfoMapper.selectPrimary(Long.valueOf(request.getTargetId()));
        NoteInfoListResponse response = new NoteInfoListResponse();
        response.success();
        NoteInfoModel model = new NoteInfoModel();
        model.setList(Lists.newArrayList());
        MarketNotice notice = new MarketNotice();
        notice.setMarketName(baseInfo.getMarketName());
        List<MarketNotice> list =marketNoticeMapper.selectList(notice);
        if(!CollectionUtils.isEmpty(list)){
            NoteInfo noteInfo;
            for(MarketNotice marketNotice:list){
                noteInfo = new NoteInfo();
                noteInfo.setCreateTime(DateUtils.format(marketNotice.getPubTime(),"yyyy/MM/dd"));
                noteInfo.setTimestamp((int) (marketNotice.getPubTime().getTime()/1000));
                noteInfo.setTitle(marketNotice.getTitleCn());
                noteInfo.setUrl(marketNotice.getBlockId());
                model.getList().add(noteInfo);
            }
        }
        response.setData(model);
        return response;
    }

    @ApiOperation(value = "note/get",notes = "获取公告详情")
    @RequestMapping(value = "note/get",method = RequestMethod.GET)
    public MarketNoticeGetResponse noteGet(@RequestParam("url")String url){
        MarketNoticeGetResponse response = new MarketNoticeGetResponse();
            response.success();
            MarketNoticeGetModel model = new MarketNoticeGetModel();
            response.setData(model);
            MarketNotice notice = new MarketNotice();
            notice.setBlockId(url);
            List<MarketNotice>  list = this.marketNoticeMapper.selectList(notice);
            if(!CollectionUtils.isEmpty(list)){
                model.setContentCn(list.get(0).getContentCn());
                model.setContentEn(list.get(0).getContentEn());
                model.setContentKr(list.get(0).getContentKr());
                model.setTitleCn(list.get(0).getTitleCn());
                model.setTitleEn(list.get(0).getTitleEn());
                model.setTitleKr(list.get(0).getTitleKr());
                model.setMarketName(list.get(0).getMarketName());
                model.setPubTime(DateUtils.format(list.get(0).getPubTime(),"yyyy-MM-dd HH:mm:ss"));
                response.setData(model);
            }
            return response;
    }


    private List<MarketListUpDownInfo> formatUpDown(List<GlobalShowInfo> list){
            List<MarketListUpDownInfo> result = new ArrayList<MarketListUpDownInfo>();
            if(!CollectionUtils.isEmpty(list)){
                MarketListUpDownInfo entity;
                for(GlobalShowInfo info:list){
                    entity = new MarketListUpDownInfo();
                    entity.setName(info.getUsName());
                    entity.setPrice(MoneyUtils.formatter(defaultSymbol()?info.getPriceCny():info.getPriceUsd(),false));
                    entity.setRange(RangeUtil.formatter(info.getPercentChange24h()));
                    entity.setSymbol(info.getSymbol());
                    entity.setTargetId(info.getTargetId());
                    result.add(entity);
                }
            }
            return result;
    }

    private List<MarketListTradeInfo> formatTradeAmountList(List<GlobalShowInfo> list){
        List<MarketListTradeInfo> result = new ArrayList<MarketListTradeInfo>();
        if(!CollectionUtils.isEmpty(list)){
            MarketListTradeInfo entity;
            CoinBaseInfo coinBaseInfo;
            for(GlobalShowInfo info:list){
                entity = new MarketListTradeInfo();
                entity.setName(info.getSymbol());
                entity.setPrice(MoneyUtils.formatter(defaultSymbol()?info.getPriceCny():info.getPriceUsd(),false));
                entity.setExchangePrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                entity.setTardeAmount(MoneyUtils.formatter(defaultSymbol()?info.getVol24hCny():info.getVol24hUsd(),true));
                entity.setTargetId(info.getTargetId());
                entity.setFullName(info.getUsName());
                coinBaseInfo = coinBaseInfoUtils.get(info.getTargetId());
                if(coinBaseInfo != null){
                    entity.setLogoUrl(coinBaseInfo.getLogo());
                    entity.setCnName(coinBaseInfo.getCnName());
                }
                result.add(entity);
            }
        }
        return result;
    }

    private List<MarketListFundInfo> formatFundList(List<GlobalShowInfo> list){
        List<MarketListFundInfo> result = new ArrayList<MarketListFundInfo>();
        if(!CollectionUtils.isEmpty(list)){
            MarketListFundInfo entity;
            CoinBaseInfo coinBaseInfo;
            for(GlobalShowInfo info:list){
                entity = new MarketListFundInfo();
                entity.setName(info.getSymbol());
                entity.setPrice(MoneyUtils.formatter(defaultSymbol()?info.getPriceCny():info.getPriceUsd(),false));
                entity.setExchangePrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                entity.setNetInflow(MoneyUtils.formatter(info.getNetflow1d(),true));
                entity.setTargetId(info.getTargetId());
                entity.setFullName(info.getUsName());
                coinBaseInfo = coinBaseInfoUtils.get(info.getTargetId());
                if(coinBaseInfo != null){
                    entity.setLogoUrl(coinBaseInfo.getLogo());
                    entity.setCnName(coinBaseInfo.getCnName());
                }
                result.add(entity);
            }
        }
        return result;
    }



    private List<MarketListNewInfo> formatNewList(List<GlobalShowInfo> list){
        List<MarketListNewInfo> result = new ArrayList<MarketListNewInfo>();
        if(!CollectionUtils.isEmpty(list)){
            MarketListNewInfo entity;
            CoinBaseInfo coinBaseInfo;
            for(GlobalShowInfo info:list){
                entity = new MarketListNewInfo();
                entity.setName(info.getSymbol());
                entity.setPrice(MoneyUtils.formatter(defaultSymbol()?info.getPriceCny():info.getPriceUsd(),false));
                entity.setExchangePrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                entity.setPublishTime(DateUtils.format(info.getShelfTime(),"yyyy/MM/dd"));
                entity.setTargetId(info.getTargetId());
                entity.setFullName(info.getUsName());
                coinBaseInfo = coinBaseInfoUtils.get(info.getTargetId());
                if(coinBaseInfo != null){
                    entity.setLogoUrl(coinBaseInfo.getLogo());
                    entity.setCnName(coinBaseInfo.getCnName());
                }
                result.add(entity);
            }
        }
        return result;
    }

    private String hasPic(String usName,String toCoin,String marketName){
        MarketCoinCoin cMarketCoinCoin = new MarketCoinCoin();
        cMarketCoinCoin.setFCoin(usName);
        cMarketCoinCoin.setTCoin(toCoin);
        cMarketCoinCoin.setMarketName(marketName);
        List<MarketCoinCoin> list = this.marketCoinCoinMapper.selectList(cMarketCoinCoin);
        if(CollectionUtils.isEmpty(list))
        return "N";
        if(list.get(0).getHasKline()!=null && list.get(0).getHasKline()==1){
            return "Y";
        }
        return "N";
    }


    private List<MarkertMoreListInfo> formatMarketMoreList(List<GlobalShowInfo> list){
        List<MarkertMoreListInfo> result = Lists.newArrayList();
        if(!CollectionUtils.isEmpty(list)){
            MarkertMoreListInfo model;
            CoinBaseInfo baseInfo;
            for(GlobalShowInfo info:list){
                baseInfo = coinBaseInfoUtils.get(info.getTargetId());
                model = new MarkertMoreListInfo();
                model.setName(info.getSymbol());
                if(defaultSymbol()){
                    model.setPrice(MoneyUtils.formatter(info.getPriceCny(),false));
                    model.setTradeAmount(MoneyUtils.formatter(info.getVol24hCny(),true));
                    model.setValueAmount(MoneyUtils.formatter(info.getMarketCapCny(),true));
                }else {
                    model.setPrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                    model.setTradeAmount(MoneyUtils.formatter(info.getVol24hUsd(),true));
                    model.setValueAmount(MoneyUtils.formatter(info.getMarketCapUsd(),true));
                }
                model.setExchangePrice(MoneyUtils.formatter(info.getPriceUsd(),false));
                model.setNetflowAmount(MoneyUtils.formatter(info.getNetflow1d(),true));
                model.setRange(RangeUtil.formatter(info.getPercentChange24h()));
                model.setTargetId(info.getTargetId());
                model.setFullName(info.getUsName());
                String objStr = redisUtils.get("G_M_PERCENT_"+info.getSymbol());
                if(objStr != null){
                    PicInfo picInfo =  JSON.parseObject(objStr,PicInfo.class);
                    if(StringUtils.isEmpty(picInfo.getKey().get(0))){
                        model.setGroomMarketName("");
                    }else{
                        model.setGroomMarketName(picInfo.getKey().get(0));
                    }
                }
                if(baseInfo != null){
                    model.setLogo(baseInfo.getLogo());
                    model.setCnName(baseInfo.getCnName());
                    if(baseInfo.getShelfTime()!= null && !baseInfo.getShelfTime().equals("None")){
                        model.setPublishTime(baseInfo.getShelfTime().replaceAll("-","/"));
                    }else{
                        model.setPublishTime("1970/01/01");
                    }
                }else{
                    model.setPublishTime("1970/01/01");
                    model.setCnName(info.getUsName());
                }
                result.add(model);
            }
        }
        return result;
    }


}
